<?php
/**
 * Created by PhpStorm.
 * User: PuLinGao
 * Date: 2014/11/06
 * Time: 16:20
 * 项目管理
 */
class ProjectController extends AdminController{
    private $pagesize = 15;
    /**
     * 项目列表
     */
    public function actionList(){
    	$p = (int)$this->get('p', 1);
        $current_status = (int)$this->get('current_status');
    	$project_name = $this->get('project_name');
    	$criteria = new CDbCriteria();
        if ($current_status) {
            $criteria->addColumnCondition(array('status' => $current_status));
            $this->smarty->assign('current_status', $current_status);
        } else {
            $criteria->addInCondition('status', array(-1,1,2));
        }
        
        if ($project_name) {
            $criteria->addSearchCondition('project_name', $project_name);
            $this->smarty->assign('project_name', $project_name);
        }
    	//某个项目权限
        if ($this->project_id!=-1) {
            $criteria->addInCondition('project_id', explode(',', $this->project_id));
        }
        $count = Project::model()->count($criteria);
    	$criteria->limit = $this->pagesize;
    	$criteria->offset = ($p - 1) * $this->pagesize;
    	$criteria->order = 'project_id DESC';
    	$data = Project::model()->findAll($criteria);

    	$page = new Page($count, $this->pagesize);
    	
    	$this->smarty->assign('status', Project::projectStatus());
    	$this->smarty->assign('page', $page->style1());
    	$this->smarty->assign('list', $data);
    	$this->smarty->display('project/list.tpl');
    }
    /**
     * 项目预览
     */
    public function actionView()
    {
        $projectId = (int)$this->get('project_id');
        //无权限
        if ($this->project_id!=-1 && stripos($projectId, $this->project_id)===false) {
           $this->redirect('error/forbid', true);
        }
        
        //管理员列表
        $group = new ManageGroup();
        $user = new ManageUser();
        $group_list = $group->findAll('status=1');
        foreach ($group_list as $key => $val) {
            //超管的话就不用列出了
            if ($val['group_id'] == 1){
                unset($group_list[$key]);
                continue;
            }
            $group_list[$key]['user'] = $user->findAll('group_id='.$val['group_id']);
        }
        $this->smarty->assign('group_list', $group_list);
        //当前项目的管理人员
        $userproject = new ManageUserProject();
        $user_list = $userproject->getByProject($projectId);
        $this->smarty->assign('user_list', $user_list);

        $projectModel = new Project();
        $info = $projectModel->findByPk($projectId);
        
        //总行
        $network = new NetWork();
        $network_level_one = $network->findByPk($info->up_network_id)->attributes;
        $network_level_second = $network->findByPk($info->network_id)->attributes;

        $this->smarty->assign('network_level_second', $network_level_second);
        $this->smarty->assign('info', $info);
        $this->smarty->assign('network_level_one', $network_level_one);
        $this->smarty->display('project/view.tpl');
    }
    /**
     * 添加或编辑项目
     */
    public function actionAdd(){
        //管理员列表
        $group = new ManageGroup();
        $user = new ManageUser();
        $group_list = $group->findAll('status=1');
        foreach ($group_list as $key => $val) {
            //超管的话就不用列出了
            if ($val['group_id'] == 1){
                unset($group_list[$key]);
                continue;
            }
            $group_list[$key]['user'] = $user->findAll('group_id='.$val['group_id']);
        }
        //总行
        $network = new NetWork();
        $network_level_one = $network->getLevel(0, 1);

        $this->smarty->assign('network_level_one', $network_level_one);
        $this->smarty->assign('group_list', $group_list);
    	$this->smarty->display('project/add.tpl');
    }

    /**
    * 处理添加
    */
    public function actionDoAdd(){
        $projectModel = new Project();
        $project_num = $this->post('project_num');
        $project_name = $this->post('project_name');
        $projectModel->project_num = $project_num;
        $projectModel->project_name = $project_name;
        $projectModel->network_id = (int)$this->post('network_id');
        $projectModel->up_network_id = (int)$this->post('up_network_id');//不能为空
        $start_time = $this->post('start_time');
        $end_time = $this->post('end_time');
        $email = $this->post('email');
        //判断编号和名称是否存在
        if (Project::model()->count("(project_num='{$project_num}' or project_name='{$project_name}') and status!=0")) {
            $this->jump('编号或名称已经存在，请修改后添加');
        }
        //判断项目的开始时间，如果开始时间大于当前说明项目已经启动运行，否则未启动
        if ($start_time <= date('Y-m-d')) {
            $projectModel->status = Project::PROJECT_RUNNING;
        } else {
            $projectModel->status = Project::PROJECT_NOMAIL;
        }
        $projectModel->start_time = $start_time;
        $projectModel->end_time = $end_time;
        $projectModel->scope = $this->post('scope', '');
        $projectModel->method = $this->post('method', '');
        $projectModel->background = $this->post('background');
        $projectModel->create_time = time();
        $projectModel->update_time = time();
	$projectModel->email = $email;
        
        $projectModel->user_id = $this->user_id ? $this->user_id : 0;
        $user_ids = $this->post('user_id');
        
        //根据总行查找银行id
        $network = new NetWork();
        $n = $network->findByPk($projectModel->up_network_id)->attributes;
        if ($n) {
            $projectModel->bank_id = intval($n['bank_id']);
        }

        $res = $projectModel->save();
        if ($res) {
            //处理用户
            if ($user_ids) {
                $userproject = new ManageUserProject();
                foreach ($user_ids as $user_id) {
                    $userproject->user_id = $user_id;
                    $userproject->project_id = $projectModel->attributes['project_id'];
                    $userproject->save();
                }
            }
            
           $this->jump('添加成功', '/webadmin/project/list');
        } else {
           $this->jump('添加失败');
        }
    }
    /**
     * 检查唯一性
     */
    public function actionCheck(){
        $project_id = (int)$this->get('project_id');
        $project_num = $this->get('project_num');
        if ($project_num && Project::model()->count("project_num='{$project_num}' and project_id!={$project_id} and status!=0")) {
            $this->_ajax(array('status' => 1,'note' => '项目编号已存在'));
        }
        $project_name = $this->get('project_name');
        if ($project_name && Project::model()->count("project_name='{$project_name}' and project_id!={$project_id} and status!=0")) {
            $this->_ajax(array('status' => 1,'note' => '项目名称已存在'));
        }
        $this->_ajax(array('status' => 0,'note' => ''));
    }

    /**
     * 编辑
     */
    public function actionEdit(){
        $projectId = (int)$this->get('project_id');
        //无权限
        if ($this->project_id!=-1 && stripos($projectId, $this->project_id)===false) {
           $this->redirect('error/forbid', true);
        }
        
        //管理员列表
        $group = new ManageGroup();
        $user = new ManageUser();
        $group_list = $group->findAll('status=1');
        foreach ($group_list as $key => $val) {
            //超管的话就不用列出了
            if ($val['group_id'] == 1){
                unset($group_list[$key]);
                continue;
            }
            $group_list[$key]['user'] = $user->findAll('group_id='.$val['group_id']);
        }
        $this->smarty->assign('group_list', $group_list);
        //当前项目的管理人员
        $userproject = new ManageUserProject();
        $user_list = $userproject->getByProject($projectId);
        $this->smarty->assign('user_list', $user_list);

        //总行
        $network = new NetWork();
        $network_level_one = $network->getLevel(0, 1);
        
        $projectModel = new Project();
        $info = $projectModel->findByPk($projectId);
        $this->smarty->assign('info', $info);
        $this->smarty->assign('network_level_one', $network_level_one);
        $this->smarty->display('project/add.tpl');
    }

    /**
     * 处理修改
     */
    public function actionDoEdit(){
            $projectId = $this->post('project_id');

            # 获取post值
            $project_num = $this->post('project_num');
            $project_name = $this->post('project_name');
            $data['project_num'] = $project_num;
            $data['project_name'] = $project_name;
            $data['network_id'] = (int)$this->post('network_id');
            $data['up_network_id'] = (int)$this->post('up_network_id');
            $data['start_time'] = $this->post('start_time');
            $data['end_time'] = $this->post('end_time');
            $data['scope'] = $this->post('scope');
            $data['method'] = $this->post('method');
            $data['background'] = $this->post('background');
            $data['update_time'] = time();
            $data['status'] = Project::PROJECT_RUNNING;
            $data['user_id'] = $this->user_id ? $this->user_id : 0;
			$data['email'] = $this->post('email');
        //判断编号和名称是否存在
        if (Project::model()->count("project_id!={$projectId} and (project_num='{$project_num}' or project_name='{$project_name}') and status!=0")) {
            $this->jump('编号或名称已经存在，请修改后添加');
        }
                        
                        
            //根据总行查找银行id
            $network = new NetWork();
            $n = $network->findByPk($data['up_network_id'])->attributes;
            if ($n) {
                $data['bank_id'] = intval($n['bank_id']);
            }
            
            $projectModel = new Project();
            $res = $projectModel->updateByPk($projectId,$data);
            if ($res) {
                $user_ids = $this->post('user_id');
                //查找有哪些用户管理此项目
                $userproject = new ManageUserProject();
                $uids = $userproject->getByProject($projectId);
                //新添加的用户
                foreach ($user_ids as $val) {   
                    if (!in_array($val, $uids)) {
                        $userproject->user_id = $val;
                        $userproject->project_id = $projectId;
                        $userproject->save();
                    }
                }
                //删除用户
                foreach ($uids as $val) {
                   if (!in_array($val, $user_ids)) {
                       $userproject->deleteAll('user_id=:user_id and project_id=:project_id', array(':user_id' => $val, ':project_id' => $projectId));
                   } 
                }
                
                $this->jump('修改成功', '/webadmin/project/list');
            } else {
                $this->jump('修改失败');
            }

    }
    /**
     * 删除项目
	 *同时删除项目下的问卷和，项目下的网点问卷
     */
    public function actionDelete(){
        $project_id = (int)$this->post('project_id');
        if (Project::model()->updateByPk($project_id, array('status' => 0))) {
            QuestionBase::model()->updateAll(array('status' => QuestionBase::QUESTION_DELETE),'project_id=:project_id',array(':project_id' => $project_id));
            NetworkQuestion::model()->updateAll(array('status' => NetworkQuestion::NETWORK_QUESTION_DELETE),'project_id=:project_id',array(':project_id' => $project_id));
            $this->_ajax(array('status' => 1,'note'=>'删除成功'));
        } else {
            $this->_ajax(array('status' => 0,'note'=>'删除失败'));
        }
    }
}